<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/72X72.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/48X48.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/32X32.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"issacyoung.cn","root":"/","scheme":"Pisces","version":"7.7.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="为什么学习PyTorch 深度学习框架太多不知道如何选择  开源源码很多，阅读和修改很吃力  解决实际工程任务，思路不清晰，不知道如何入手  项目实战 cnn 卷积 rnn 循环网络 gan  模型保存与部署、调参技巧、优化思想   前置知识  机器学习基本概念 Python编程基础 线性代数、概率论 Linux编程  学习资料  机器学习白皮书  线性代数  统计学习方法  深度学习花书  吴恩">
<meta property="og:type" content="article">
<meta property="og:title" content="一步一步之PyTorch">
<meta property="og:url" content="http://issacyoung.cn/2024/03/07/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BPyTorch/index.html">
<meta property="og:site_name" content="Young‘s River">
<meta property="og:description" content="为什么学习PyTorch 深度学习框架太多不知道如何选择  开源源码很多，阅读和修改很吃力  解决实际工程任务，思路不清晰，不知道如何入手  项目实战 cnn 卷积 rnn 循环网络 gan  模型保存与部署、调参技巧、优化思想   前置知识  机器学习基本概念 Python编程基础 线性代数、概率论 Linux编程  学习资料  机器学习白皮书  线性代数  统计学习方法  深度学习花书  吴恩">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="c:%5CUsers%5Cadmin%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20240413215137646.png">
<meta property="article:published_time" content="2024-03-07T04:25:21.000Z">
<meta property="article:modified_time" content="2024-04-21T15:22:32.179Z">
<meta property="article:author" content="Issac Young">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:%5CUsers%5Cadmin%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20240413215137646.png">

<link rel="canonical" href="http://issacyoung.cn/2024/03/07/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BPyTorch/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>一步一步之PyTorch | Young‘s River</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="Young‘s River" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Young‘s River</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">一个正能量的程序员，喜欢码字，热爱运动，向往自由</p>
  </div>

  <div class="site-nav-right"></div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/IssacYoung2013" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://issacyoung.cn/2024/03/07/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BPyTorch/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Issac Young">
      <meta itemprop="description" content="现代生活工作节奏越来越快，需要有时间和空间沉淀积累，将知识点规划成树，记录自己的感悟心得。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Young‘s River">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          一步一步之PyTorch
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2024-03-07 12:25:21" itemprop="dateCreated datePublished" datetime="2024-03-07T12:25:21+08:00">2024-03-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2024-04-21 23:22:32" itemprop="dateModified" datetime="2024-04-21T23:22:32+08:00">2024-04-21</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.9k</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="为什么学习PyTorch"><a href="#为什么学习PyTorch" class="headerlink" title="为什么学习PyTorch"></a>为什么学习PyTorch</h2><ul>
<li><p>深度学习框架太多不知道如何选择</p>
</li>
<li><p>开源源码很多，阅读和修改很吃力</p>
</li>
<li><p>解决实际工程任务，思路不清晰，不知道如何入手</p>
</li>
<li><p>项目实战 cnn 卷积 rnn 循环网络 gan</p>
</li>
<li><p><strong>模型保存与部署、调参技巧、优化思想</strong></p>
</li>
</ul>
<p><strong>前置知识</strong></p>
<ul>
<li>机器学习基本概念</li>
<li>Python编程基础</li>
<li>线性代数、概率论</li>
<li>Linux编程</li>
</ul>
<p><strong>学习资料</strong></p>
<ul>
<li><p>机器学习白皮书</p>
</li>
<li><p>线性代数</p>
</li>
<li><p>统计学习方法</p>
</li>
<li><p>深度学习花书</p>
</li>
<li><p>吴恩达视频</p>
</li>
</ul>
<h2 id="初识-PyTorch"><a href="#初识-PyTorch" class="headerlink" title="初识 PyTorch"></a>初识 PyTorch</h2><p>Facebook 2007年开源，Torch -&gt; PyTorch。</p>
<h3 id="PyTorch-VS-TensorFlow"><a href="#PyTorch-VS-TensorFlow" class="headerlink" title="PyTorch VS TensorFlow"></a>PyTorch VS TensorFlow</h3><p>PyTorch</p>
<ul>
<li>简洁性（编程同Python几乎一致）</li>
<li>动态计算</li>
<li>visdom</li>
<li>部署不方便，看成 python后台服务，比如flask</li>
</ul>
<p>TensorFlow 1.0</p>
<ul>
<li>接口复杂</li>
<li>静态图（2.0 Eager Execution 已经引入动态图）</li>
<li>Tensorboard（可视化）</li>
<li>部署方便 TF serving（专门针对Tensorflow 模型调度更好）</li>
</ul>
<h3 id="静态图与动态图"><a href="#静态图与动态图" class="headerlink" title="静态图与动态图"></a>静态图与动态图</h3><p><strong>编程语言的执行本身就是个图的遍历</strong></p>
<p>动态图：编好程序即可执行</p>
<p>静态图：先搭建计算图，后运行；允许编译器进行优化</p>
<h3 id="PyTorch环境搭建"><a href="#PyTorch环境搭建" class="headerlink" title="PyTorch环境搭建"></a>PyTorch环境搭建</h3><p>Ubuntu16.04 、CUDA+cuDNN、Python3+pip3 Anaconda、PyTorch</p>
<ul>
<li><p>为什么选择 Ubuntu</p>
<ul>
<li>安装双系统，不要使用虚拟机</li>
<li>参考安装教程：<a href="https://www.php.cn/linux-373791.html" target="_blank" rel="noopener">https://www.php.cn/linux-373791.html</a></li>
</ul>
</li>
<li><p>CUDA</p>
<ul>
<li>CUDA9.0 10.0 10.1</li>
<li>cuDNN</li>
<li><a href="https://www.imooc.com/article/303675" target="_blank" rel="noopener">https://www.imooc.com/article/303675</a></li>
</ul>
</li>
<li><p>Python3 OR Anaconda</p>
<ul>
<li>Python3 + pip3安装所需的依赖包</li>
<li>Anaconda 安装源（Python全家桶）</li>
</ul>
</li>
<li><p>加速镜像</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 添加清华源的pytorch</span></span><br><span class="line">conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/</span><br><span class="line">conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/</span><br><span class="line">conda config --<span class="built_in">set</span> show_channel_urls yes</span><br><span class="line">conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/</span><br><span class="line"></span><br><span class="line">conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c nvidia  <span class="comment"># 删除安装命令最后的 -c pytorch，才会采用清华源安装。</span></span><br></pre></td></tr></table></figure>



</li>
</ul>
<h2 id="PorTorch-基础"><a href="#PorTorch-基础" class="headerlink" title="PorTorch 基础"></a>PorTorch 基础</h2><h3 id="机器学习基本构成元素"><a href="#机器学习基本构成元素" class="headerlink" title="机器学习基本构成元素"></a>机器学习基本构成元素</h3><ul>
<li>分类与回归问题<ul>
<li>图像分类，离散值，向量 [0.1,0.1,0.1,0.1,0,0,0,5,0.1,0] 10维向量，概率分布，概率值加在一起是1，离散预测是分类问题，</li>
<li>股票价格波动曲线，连续值的预测，股票价格、身高预测</li>
</ul>
</li>
<li>构成元素<ul>
<li>模型，f(x)</li>
<li>样本，挖掘，y = f(x)，包括属性x（样本描述）和标签y</li>
<li>训练，比如 f(x) = wx + b ，求解的过程，深度学习过程中参数比样本多；</li>
<li>推理，学习，计算标签的过程，拿到一堆x</li>
<li>测试，模型性能评估，ROC曲线</li>
</ul>
</li>
</ul>
<h3 id="PorTorch-基本概念"><a href="#PorTorch-基本概念" class="headerlink" title="PorTorch 基本概念"></a>PorTorch 基本概念</h3><ul>
<li>Tensor 张量 <code>h*w*c</code> 任意维度的数据，标量（数字 0阶张量1,2,3）、向量（一维表格 1阶张量 [1,2,13]）、矩阵（二阶张量，二维表格），更加泛化的概念</li>
<li>Variable autograd 变量，表达参数，对于想要求解的模型，未知数</li>
<li>nn.Module 封装解决计算机视觉需要的网路结构</li>
</ul>
<h3 id="Tensor-基本概念"><a href="#Tensor-基本概念" class="headerlink" title="Tensor 基本概念"></a>Tensor 基本概念</h3><ul>
<li><p>任意维度的数据</p>
<p><img src="C:%5CUsers%5Cadmin%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20240413215137646.png" alt="image-20240413215137646"></p>
</li>
<li><p>样本 + 模型</p>
<ul>
<li>Tensor 对样本描述</li>
<li>Y = WX + b，未知数跟变量都是 Tensor</li>
</ul>
</li>
</ul>
<h4 id="类型"><a href="#类型" class="headerlink" title="类型"></a>类型</h4><ul>
<li>torch.float32、torch.float64、torch.float16</li>
</ul>
<h4 id="创建"><a href="#创建" class="headerlink" title="创建"></a>创建</h4><ul>
<li>Tensor(*size) 基础构造函数</li>
<li>Tensor(data) 类似np.array</li>
<li>ones(*size) 全1Tensor</li>
<li>zeros(*size) 全0Tensor</li>
<li>eye(*size) 对角线1，其他为0</li>
<li>arange(s,e,step)，从s到e，步长是step</li>
</ul>
<h4 id="属性"><a href="#属性" class="headerlink" title="属性"></a>属性</h4><ul>
<li>torch.dtype、torch.device（存储的设备cpu还是gpu） 、torch.layout （内存布局的对象）三种属性</li>
<li>torch.tensor([1,2,3],dbtype=torch.float32,device=torch.device(‘cpu’))</li>
<li>稀疏的张量 torch.sparse_coo_tensor coo类型表示了非零元素的坐标形式，稀疏表示了非零元素的个数，矩阵的秩（线性可表示），稀疏是模型变得简单，参数为0的元素可以消掉；减少内存消耗</li>
</ul>
<h4 id="运算"><a href="#运算" class="headerlink" title="运算"></a>运算</h4><ul>
<li>加法运算 add 或者 + ，加 _ 会对a的值修改</li>
<li>哈达玛积（对应元素相乘）mul 或者 * <strong>容易跟矩阵运算搞错</strong></li>
<li>除法 div</li>
<li>矩阵运算<ul>
<li>二维矩阵 torch.mm() torch.matmul()</li>
<li>高维的 Tensor （dim&gt;2）矩阵乘法仅在最后两个维度上，要求前面维度必须保持一致</li>
</ul>
</li>
<li>幂运算<ul>
<li>pow(a,2) 或者 <code>**</code> 下划线 </li>
</ul>
</li>
<li>开方运算<ul>
<li>sqrt()</li>
</ul>
</li>
<li>对数运算<ul>
<li>log2(a)</li>
</ul>
</li>
</ul>
<h4 id="广播机制"><a href="#广播机制" class="headerlink" title="广播机制"></a>广播机制</h4><ul>
<li>in-place 操作</li>
<li>广播机制 张量参数可以自动扩展为相同大小</li>
<li>需要满足两个条件<ul>
<li>每个张量至少有一个维度</li>
<li>满足右对齐</li>
<li>torch.rand(2,1,3) + torch.rand(3)， 补足 torch(1,1,3)</li>
</ul>
</li>
</ul>
<h4 id="取整-取余"><a href="#取整-取余" class="headerlink" title="取整/取余"></a>取整/取余</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line">a = torch.rand(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line">a = a * <span class="number">10</span></span><br><span class="line">print(a)</span><br><span class="line">print(torch.floor(a))</span><br><span class="line">print(torch.ceil(a))</span><br><span class="line">print(torch.round(a))</span><br><span class="line">print(torch.trunc(a))</span><br><span class="line">print(torch.frac(a))</span><br><span class="line">print(a % <span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<h4 id="比较eq"><a href="#比较eq" class="headerlink" title="比较eq"></a>比较eq</h4><p>取前K大/前K小/第K小的数值</p>
<p>判断是否为finite（有界）/inf（无界）/nan</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line">a = torch.rand(<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line">b = torch.rand(<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line">print(a)</span><br><span class="line">print(b)</span><br><span class="line"></span><br><span class="line">print(torch.eq(a,b))</span><br><span class="line">print(torch.equal(a,b))</span><br><span class="line">print(torch.ge(a,b))</span><br><span class="line">print(torch.gt(a,b))</span><br><span class="line">print(torch.le(a,b))</span><br><span class="line">print(torch.lt(a,b))</span><br><span class="line">print(torch.ne(a,b))</span><br><span class="line"></span><br><span class="line"><span class="comment"># sort</span></span><br><span class="line">a = torch.tensor([[<span class="number">1</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">5</span>],[<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]])</span><br><span class="line">print(a.shape)</span><br><span class="line">print(torch.sort(a,dim=<span class="number">0</span>,descending=<span class="literal">True</span>))</span><br><span class="line"><span class="comment"># topk</span></span><br><span class="line">a = torch.tensor([[<span class="number">2</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">1</span>,<span class="number">5</span>],[<span class="number">2</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">1</span>,<span class="number">4</span>]])</span><br><span class="line">print(a.shape)</span><br><span class="line"></span><br><span class="line">print(torch.topk(a,k=<span class="number">2</span>,dim=<span class="number">0</span>))</span><br><span class="line"></span><br><span class="line">a = torch.rand(<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line">print(a)</span><br><span class="line">print(torch.isfinite(a))</span><br><span class="line">print(torch.isfinite(a/<span class="number">0</span>))</span><br><span class="line">print(torch.isinf(a/<span class="number">0</span>))</span><br><span class="line">print(torch.isnan(a))</span><br></pre></td></tr></table></figure>

<h4 id="三角函数"><a href="#三角函数" class="headerlink" title="三角函数"></a><strong>三角函数</strong></h4><p>相似度用cos函数比较，越小相似度越高</p>
<p>预测和角度有关系的</p>
<h4 id="其他数学函数"><a href="#其他数学函数" class="headerlink" title="其他数学函数"></a><strong>其他数学函数</strong></h4><ul>
<li>abs （绝对值，没办法求导，l1 loss）、sigmoid（激活函数，越小趋近于0，越大趋近于1，外观上同符号函数相似，是符号函数连续）、sign（符号函数，分段函数，机器学习中提到的分类问题，对输出结果数据离散化，不是连续函数，不能求导优化）</li>
</ul>
<h4 id="统计学相关的函数"><a href="#统计学相关的函数" class="headerlink" title="统计学相关的函数"></a><strong>统计学相关的函数</strong></h4><ul>
<li>mean 平均值、sum 返回总和、prod 所有元素的积、max 最大值、min 最小值、argmax 最大值索引、argmin 最小值索引</li>
<li>std 返回标准差、var 方差、median 中间值、mode 众数值、histc 直方图（表达数据统计的，对旋转不敏感，不鲁棒）、bincount 每个值的频数</li>
</ul>
<h4 id="分布函数"><a href="#分布函数" class="headerlink" title="分布函数"></a>分布函数</h4><ul>
<li>distributions 包含可参数化的概率分布和采样函数<ul>
<li>得分函数<ul>
<li>强化学习中策略梯度方法的基础</li>
</ul>
</li>
<li>pathwise derivative 估计器<ul>
<li>变分自动编码器中的重新参数化技巧</li>
</ul>
</li>
</ul>
</li>
<li>Bernoulli 、Beta 、Binomial 、OneHotCategorical、TransformedDistribution 等等分布函数</li>
<li>KL Divergence 度量两种分布的差异</li>
<li>Transforms 两种分布之间的转换</li>
<li>Constraint 分布的约束</li>
</ul>
<h4 id="随机抽样"><a href="#随机抽样" class="headerlink" title="随机抽样"></a>随机抽样</h4><ul>
<li>定义随机种子<ul>
<li>torch.manual_seed(seed)</li>
</ul>
</li>
<li>定义随机数满足的分布<ul>
<li>torch.normal()</li>
</ul>
</li>
</ul>
<h4 id="范数运算"><a href="#范数运算" class="headerlink" title="范数运算"></a>范数运算</h4><ul>
<li>范数<ul>
<li>在泛函分析中，它定义在赋范线性空间中，并满足一定的条件，即1非负性；2齐次性；3三角不等式</li>
<li>常被用来度量某个向量空间（或矩阵）中每个向量的<strong>长度或大小</strong></li>
</ul>
</li>
<li>0范数/1范数（绝对值和）/2范数（平方和）/p范数/核范数<ul>
<li>torch.dist(input,other,p =2) 计算p范数</li>
<li>torch.norm() 计算2范数</li>
</ul>
</li>
</ul>
<h4 id="矩阵分解"><a href="#矩阵分解" class="headerlink" title="矩阵分解"></a>矩阵分解</h4><p><strong>常见的矩阵分解</strong></p>
<ul>
<li>LU分解：将矩阵A分解成L（下三角）矩阵和U（上三角）矩阵的乘积</li>
<li>QR分解：将原矩阵分解成一个正交矩阵Q和一个上三角矩阵R的乘积</li>
<li>EVD分解：特征值分解<ul>
<li>矩阵方阵且满秩（可对角化）</li>
<li>矩阵分解不等于特征降维度</li>
<li>协方差矩阵描述方法和相关性</li>
</ul>
</li>
<li>SVD分解：奇异值分解<ul>
<li>torch.svd()</li>
</ul>
</li>
<li>特征值分解<ul>
<li>矩阵分解为由其特征值和特征向量表示的矩阵之积的方法</li>
<li>特征值 VS 特征向量</li>
</ul>
</li>
</ul>
<h4 id="裁剪运算"><a href="#裁剪运算" class="headerlink" title="裁剪运算"></a>裁剪运算</h4><ul>
<li>对 Tensor 的元素进行范围内过滤<ul>
<li>稳定，正则化，防止过拟合，</li>
<li>loss </li>
</ul>
</li>
<li>常用于梯度裁剪（gradient clipping），即在发生梯度离散或者梯度爆炸时对梯度的处理（x的n次方， x&gt;1 n-&gt;∞ f(x)-&gt;∞ ，x&lt;1，n-&gt;∞，f(x) -&gt;0）</li>
</ul>
<h4 id="索引和数据筛选"><a href="#索引和数据筛选" class="headerlink" title="索引和数据筛选"></a>索引和数据筛选</h4><ul>
<li><p>torch.where(condition,x,y)：按照条件从x和y中选出满足条件元素组成新的tensor</p>
</li>
<li><p>torch.gather(input,dim,index,out=None) 指定维度上按照索引赋值输出tensor</p>
</li>
<li><p>torch.index_select 按照指定索引输出tensor</p>
</li>
<li><p>torch.masked_select 按照 mask输出tensor，输出为向量</p>
</li>
<li><p>torch.take</p>
</li>
<li><p>操作，切片、索引、变形</p>
</li>
<li><p>numpy（可以定义任意维度数据）的相互转换</p>
</li>
</ul>
<h3 id="神经网络基本概念"><a href="#神经网络基本概念" class="headerlink" title="神经网络基本概念"></a>神经网络基本概念</h3><ul>
<li>人工智能领域 -&gt; 机器学习 -&gt; 神经网络 <strong>深度学习</strong>（更多层）</li>
<li>神经网络如下图所示：输入层（神经元）、输出层、隐藏层、<strong>多层感知器</strong></li>
</ul>
<h4 id="感知器基本概念"><a href="#感知器基本概念" class="headerlink" title="感知器基本概念"></a>感知器基本概念</h4><ul>
<li>神经元、感知器、阶跃函数（分类）、激活函数</li>
</ul>
<p>神经网络 VS 深度学习</p>
<ul>
<li>多层感知器 -&gt; 神经网络</li>
<li>多隐层的多层感知器-&gt;深度学习<ul>
<li>CNN</li>
<li>RNN</li>
</ul>
</li>
</ul>
<h4 id="前向运算"><a href="#前向运算" class="headerlink" title="前向运算"></a>前向运算</h4><ul>
<li>计算输出值的过程 <strong>前向传播</strong>，推理 参数已知</li>
</ul>
<h4 id="反向传播"><a href="#反向传播" class="headerlink" title="反向传播"></a>反向传播</h4><ul>
<li>神经网络训练方法</li>
<li>反向传播方法，通过计算输出层与真实值之间的偏差来进行逐层调节参数<ul>
<li>参数更新多少？ 导数和学习率，真实值和预测值的偏差成为损失</li>
</ul>
</li>
</ul>
<h4 id="分类和回归"><a href="#分类和回归" class="headerlink" title="分类和回归"></a>分类和回归</h4><h4 id="过拟合和欠拟合"><a href="#过拟合和欠拟合" class="headerlink" title="过拟合和欠拟合"></a>过拟合和欠拟合</h4><ul>
<li>过拟合，给定训练集的样本表现好，但是在测试集效果差</li>
<li>欠拟合（高偏差），模型拟合不够，在训练集上表现效果差，没有充分的利用数据，预测的准确度低</li>
<li>偏差（Bias） 反应的是模型在样本上的输出与真实值之间误差，即模型本身的精确度</li>
<li>方差（Variance）反应的时模型的每一次输出结果与模型输出期望之间的误差，即模型的稳定性</li>
</ul>
<p><strong>如何防止</strong></p>
<ul>
<li>防止过拟合<ul>
<li><strong>补充数据集（数据增强）</strong></li>
<li><strong>减少模型参数</strong></li>
<li>Dropout</li>
<li>Earlystopping 早停</li>
<li>正则化&amp;稀疏化</li>
</ul>
</li>
<li>防止欠拟合<ul>
<li><strong>加大模型参数</strong></li>
<li>减少正则化参数</li>
<li><strong>更充分的训练</strong></li>
</ul>
</li>
</ul>
<h4 id="正则化问题"><a href="#正则化问题" class="headerlink" title="正则化问题"></a>正则化问题</h4><ul>
<li>L0，L1，L2 无穷范数、核范数<ul>
<li>Pytorch 通过 weight_decay 实现</li>
</ul>
</li>
<li>Dropout<ul>
<li>nn.Dropout(p=0.5)</li>
</ul>
</li>
</ul>

    </div>

    
    
    
<div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

  
</div>
        <div class="reward-container">
  <div>辛苦码字分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="Issac Young 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="Issac Young 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Issac Young
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://issacyoung.cn/2024/03/07/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BPyTorch/" title="一步一步之PyTorch">http://issacyoung.cn/2024/03/07/一步一步之PyTorch/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

            <div class="social-item">
              <a target="_blank" class="social-link" href="/atom.xml">
                <span class="icon">
                  <i class="fa fa-rss"></i>
                </span>

                <span class="label">RSS</span>
              </a>
            </div>
    </div>
  </div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2024/02/26/%E4%BC%9A%E8%AE%AE%E7%BA%AA%E8%A6%81/" rel="prev" title="会议纪要">
      <i class="fa fa-chevron-left"></i> 会议纪要
    </a></div>
      <div class="post-nav-item">
    <a href="/2024/04/21/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8Bjava%E5%B8%B8%E8%A7%81%E5%9D%91/" rel="next" title="一步一步之java常见坑">
      一步一步之java常见坑 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#为什么学习PyTorch"><span class="nav-number">1.</span> <span class="nav-text">为什么学习PyTorch</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#初识-PyTorch"><span class="nav-number">2.</span> <span class="nav-text">初识 PyTorch</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#PyTorch-VS-TensorFlow"><span class="nav-number">2.1.</span> <span class="nav-text">PyTorch VS TensorFlow</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#静态图与动态图"><span class="nav-number">2.2.</span> <span class="nav-text">静态图与动态图</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PyTorch环境搭建"><span class="nav-number">2.3.</span> <span class="nav-text">PyTorch环境搭建</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#PorTorch-基础"><span class="nav-number">3.</span> <span class="nav-text">PorTorch 基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#机器学习基本构成元素"><span class="nav-number">3.1.</span> <span class="nav-text">机器学习基本构成元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PorTorch-基本概念"><span class="nav-number">3.2.</span> <span class="nav-text">PorTorch 基本概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Tensor-基本概念"><span class="nav-number">3.3.</span> <span class="nav-text">Tensor 基本概念</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#类型"><span class="nav-number">3.3.1.</span> <span class="nav-text">类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#创建"><span class="nav-number">3.3.2.</span> <span class="nav-text">创建</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#属性"><span class="nav-number">3.3.3.</span> <span class="nav-text">属性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#运算"><span class="nav-number">3.3.4.</span> <span class="nav-text">运算</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#广播机制"><span class="nav-number">3.3.5.</span> <span class="nav-text">广播机制</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#取整-取余"><span class="nav-number">3.3.6.</span> <span class="nav-text">取整&#x2F;取余</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#比较eq"><span class="nav-number">3.3.7.</span> <span class="nav-text">比较eq</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#三角函数"><span class="nav-number">3.3.8.</span> <span class="nav-text">三角函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#其他数学函数"><span class="nav-number">3.3.9.</span> <span class="nav-text">其他数学函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#统计学相关的函数"><span class="nav-number">3.3.10.</span> <span class="nav-text">统计学相关的函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#分布函数"><span class="nav-number">3.3.11.</span> <span class="nav-text">分布函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#随机抽样"><span class="nav-number">3.3.12.</span> <span class="nav-text">随机抽样</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#范数运算"><span class="nav-number">3.3.13.</span> <span class="nav-text">范数运算</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#矩阵分解"><span class="nav-number">3.3.14.</span> <span class="nav-text">矩阵分解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#裁剪运算"><span class="nav-number">3.3.15.</span> <span class="nav-text">裁剪运算</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#索引和数据筛选"><span class="nav-number">3.3.16.</span> <span class="nav-text">索引和数据筛选</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#神经网络基本概念"><span class="nav-number">3.4.</span> <span class="nav-text">神经网络基本概念</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#感知器基本概念"><span class="nav-number">3.4.1.</span> <span class="nav-text">感知器基本概念</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#前向运算"><span class="nav-number">3.4.2.</span> <span class="nav-text">前向运算</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#反向传播"><span class="nav-number">3.4.3.</span> <span class="nav-text">反向传播</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#分类和回归"><span class="nav-number">3.4.4.</span> <span class="nav-text">分类和回归</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#过拟合和欠拟合"><span class="nav-number">3.4.5.</span> <span class="nav-text">过拟合和欠拟合</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#正则化问题"><span class="nav-number">3.4.6.</span> <span class="nav-text">正则化问题</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Issac Young"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Issac Young</p>
  <div class="site-description" itemprop="description">现代生活工作节奏越来越快，需要有时间和空间沉淀积累，将知识点规划成树，记录自己的感悟心得。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">26</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/IssacYoung2013" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;IssacYoung2013" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:issacyoung@msn.cn" title="E-Mail → mailto:issacyoung@msn.cn" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Issac Young</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">64k</span>
</div>


        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/clicklove.js"></script>

